﻿using System;
using PgBirdie.Core.Model.Database.Structure;

namespace PgBirdie.Core.Model.Database.AccessControl
{
    public class PgOwner
    {
        public string Id { get; private set; }
        public PgRole Role { get; private set; }
        public PgDbObject Target { get; private set; }

        public PgOwner(PgRole role, PgDbObject target)
        {
            Role = role;
            Target = target;
            Id = GetId();
        }

        private string GetId()
        {
            var prefix = String.Empty;

            if (Target is PgSchema)
            {
                var schema = (PgSchema)Target;
                prefix = String.Format("{0}|", schema.Name);
            }
            else if (Target is PgTable)
            {
                var table = (PgTable)Target;
                prefix = String.Format("{0}|{1}|", table.Schema.Name, table.Name);
            }

            return String.Format("{0}{1}", prefix, Role.Name);
        }
    }
}